<?php

use Carbon\Carbon;

class BookingController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $today = date('Y-m-d');

        if (Input::get('old') === 'yes') {

            $values = DB::select('select r.*, r.id as rentalId, s.status as Bstatus, c.Make, c.License,b.*
            from rentals as r left join cars as c on c.id=r.vehicle
            left join online_reservation as b on b.resID=r.onlineResId
            left join status as s on s.id=b.statusID where r.onlineResId IS NOT NULL
            ');

        } elseif (Input::get('pending') === 'yes') {

            $values = DB::select('select b.*,o.startDate as STARTTIME,o.endDate as ENDTIME,o.prepaid as prepaid, o.totalPrice as totalpaid,o.curency ,
                                 cu.cusFirstName as drfirstname, cu.cusLastName as drlastname,
                                 s.status as Bstatus, c.Make, c.License
            from online_reservation as b
            left join cars as c on c.id=b.vehicleID
            left join status as s on s.id=b.statusID
            left join online_booking as o on o.bookID=b.bookID
            left join online_customers as cu on cu.cusID=customerID
            where b.statusID=?', array(1));
        } else {

            $values = DB::select('select r.*, r.id as rentalId,s.status as Bstatus, c.Make, c.License,b.*
            from rentals as r
            left join cars as c on c.id=r.vehicle
            left join online_reservation as b on b.resID=r.onlineResId
            left join status as s on s.id=b.statusID
            where r.STARTTIME >= ? AND b.statusID=? AND r.onlineResId IS NOT NULL', array($today, 4));
        }

        return View::make('pages.online_reservations', array(
                    'values' => $values,
        ));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        
    }

    public function online_invoice($id) {
        $data = DB::select('select r.*,r.id as rentalId, o.*, r.*, c.cusFirstName, '
                        . 'c.cusLastName, '
                        . 'cr.Make, cr.Model, cr.License, cr.vin '
                        . 'from rentals as r '
                        . 'left join online_reservation as o on o.resID=r.onlineResId '
                        . 'left join online_customers as c '
                        . 'on c.cusID=o.customerID '
                        . 'left join cars as cr '
                        . 'on cr.id=o.vehicleID '
                        . 'where r.id = ? ', array($id));

        return View::make('pages.invoice', array(
            'data' => $data,
                    'invoice_id' => $data[0]->rentalId,
                    'invoice_date' => $data[0]->created_at,
                    'due_date' => $data[0]->updated_at,
                    'qty' => $data[0]->rentdays,
                    'price' => $data[0]->rate,
                    'make' => $data[0]->Make,
                    'model' => $data[0]->Model,
                    'amount' => $data[0]->totalpaid,
                    'amountdue' => $data[0]->amountdue,
                    'prepaid' => $data[0]->prepaid,
                    'vehicle_id' => $data[0]->vehicle,
                    'cfname' => $data[0]->cusFirstName,
                    'clname' => $data[0]->cusLastName,
                    'curency' => $data[0]->curency,
                    'startdate' => $data[0]->STARTTIME,
                    'enddate' => $data[0]->ENDTIME,
                    'status' => $data[0]->invoice_status
        ));
    }

    public function booking_history() {
        $car_update = DB::select('select r.*, u.username, c.make, c.license '
                        . 'from revisions as r left join users as u on u.id=r.user_id '
                        . 'left join cars as c on c.id = r.revisionable_id '
                        . 'where r.revisionable_type="Car"');

        return View::make('pages.history', array(
                    'car_update' => $car_update,
//                    'insurdoc_update' => $insurdoc_update,
//                    'repair_parts' => $repair_parts
        ));
    }

    public function online_logs() {
        $online_logs = DB::select('select * from online_logs as o '
                        . 'left join online_customers as c '
                        . 'on c.cusID=o.customerID '
                        . 'left join cars as cr '
                        . 'on cr.id=o.vehicleID');

        return View::make('pages.online_logs', array(
                    'online_logs' => $online_logs,
        ));
    }

}
